package de.tum.in.eist.poll.shared.dao;

import java.util.ArrayList;
import java.util.TreeMap;

import de.tum.in.eist.poll.shared.beans.User;

/**
 * @author Alexander Rosenblit
 * @author Long Huynh Huu <long.upcase@googlemail.com>
 *
 */

public class UserDaoImpl implements UserDaoInterface {
	private TreeMap<String,User> users = new TreeMap<String,User>();
	
	public UserDaoImpl() {
	}

	@Override
	public void add(User s) {
		users.put(s.getUserID(), s);
	}


	@Override
	public User get(String userID) {
		return users.get(userID);
	}

	@Override
	public ArrayList<User> getAll() {
		ArrayList<User> result = new ArrayList<User>();
		for (String userID: users.keySet()) {
			result.add(users.get(userID));
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see de.tum.in.eist.poll.server.PersistentUser#exists(int)
	 */
	@Override
	public boolean exists(String userID) {
		return users.containsKey(userID);
	}

	/* (non-Javadoc)
	 * @see de.tum.in.eist.poll.server.PersistentUser#deleteUser(int)
	 */
	@Override
	public void delete(String userID) {
		if (exists(userID)) {
			users.remove(userID);
		}
	}
}